﻿package com.athena.process
{
	import flash.display.MovieClip;
	import flash.display.*;
	import flash.text.TextFormat;
	import flash.utils.*;
	import flash.events.*;
	import fl.containers.ScrollPane; 
	import fl.controls.ScrollPolicy; 
	import com.adobe.serialization.json.JSON;
	import com.athena.controller.MailItemController;
	/**
	 * ...
	 * @author ...
	 */
	public class MailFunction 
	{
		public static var arrMailAdmin : Array = null ;
		public static var arrMailPlayer : Array = null ;
		public static var arrMailTransfer : Array = null;
		//public static var MailAdmin_List : MovieClip = new MovieClip();
		//public static var MailPlayer_List : MovieClip = new MovieClip() ;
		//public static var MailTransfer_List : MovieClip = new MovieClip() ;
		//public static var scrMailAdmin : ScrollPane = new ScrollPane(); 
		//public static var scrMailPlayer : ScrollPane = new ScrollPane(); 
		//public static var scrMailTransfer : ScrollPane = new ScrollPane(); 
		
		public static var arrReceiveAG : Array = new Array() ;
		
		public static var currentPageAdmin : int ;
		public static var currentPagePlayer : int ;
		public static var currentPageTransfer : int ;
		public static var totalPageAdmin : int ;
		public static var totalPagePlayer : int ;
		public static var totalPageTransfer : int ;
		
		public static var currentTab : String ;
		
		public static function Send_Click(evt : MouseEvent) {
			var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Mail")) ;
			if (mc == null) return ;
			mc.Send_mc.visible = true ;
			mc.MailDetail_mc.visible = false ;
			mc.Send_btn.gotoAndStop(2) ;
			mc.Admin_btn.gotoAndStop(1) ;
			mc.Transfer_btn.gotoAndStop(1) ;
			mc.Player_btn.gotoAndStop(1) ;			
		}
		public static function Admin_Click(evt : MouseEvent) {
			var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Mail")) ;
			if (mc == null) return ;
			currentTab = "Admin" ;
			mc.Send_mc.visible = false ;
			mc.Admin_mc.visible = true ;
			mc.Transfer_mc.visible = false ;
			mc.Player_mc.visible = false ;
			mc.AdminTitle_mc.visible = true ;
			mc.TransferTitle_mc.visible = false ;
			mc.PlayerTitle_mc.visible = false ;
			mc.MailDetail_mc.visible = true ;
			mc.MailDetail_mc.Id_txt.text = "" ;
			mc.MailDetail_mc.From_txt.text = "" ;
			mc.MailDetail_mc.Content_txt.text = "" ;
			mc.MailDetail_mc.Time_txt.text = "" ;
			mc.MailDetail_mc.AG_txt.text = "0" ;
			mc.MailDetail_mc.Reply_btn.visible = false ;
			mc.MailDetail_mc.ReceiveAG_btn.visible = false ;
			mc.Send_btn.gotoAndStop(1) ;
			mc.Admin_btn.gotoAndStop(2) ;
			mc.Transfer_btn.gotoAndStop(1) ;
			mc.Player_btn.gotoAndStop(1) ;
			var obj : Object = new Object() ;
			obj.evt = "15" ;
			obj.T = 4;
			obj.P = currentPageAdmin ;
			Cocaro.mainSocket.sendService(JSON.encode(obj)) ;
			//Cocaro.tf.appendText("=>SendMailAdmin:" + JSON.encode(obj)) ;
		}
		public static function Transfer_Click(evt : MouseEvent) {
			var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Mail")) ;
			if (mc == null) return ;
			currentTab = "Transfer" ;
			mc.Send_mc.visible = false ;
			mc.Admin_mc.visible = false ;
			mc.Transfer_mc.visible = true ;
			mc.Player_mc.visible = false ;
			mc.AdminTitle_mc.visible = false ;
			mc.TransferTitle_mc.visible = true ;
			mc.PlayerTitle_mc.visible = false ;
			mc.MailDetail_mc.visible = true ;
			mc.MailDetail_mc.Id_txt.text = "" ;
			mc.MailDetail_mc.From_txt.text = "" ;
			mc.MailDetail_mc.Content_txt.text = "" ;
			mc.MailDetail_mc.Time_txt.text = "" ;
			mc.MailDetail_mc.AG_txt.text = "0" ;
			mc.MailDetail_mc.Reply_btn.visible = false ;
			mc.MailDetail_mc.ReceiveAG_btn.visible = false ;
			mc.Send_btn.gotoAndStop(1) ;
			mc.Admin_btn.gotoAndStop(1) ;
			mc.Transfer_btn.gotoAndStop(2) ;
			mc.Player_btn.gotoAndStop(1) ;
			var obj : Object = new Object() ;
			obj.evt = "15" ;
			obj.T = 6;
			obj.P = currentPageTransfer ;
			Cocaro.mainSocket.sendService(JSON.encode(obj)) ;
			//Cocaro.tf.appendText("=>SendMailTransfer:" + JSON.encode(obj)) ;
		}
		public static function Player_Click(evt : MouseEvent) {
			var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Mail")) ;
			if (mc == null) return ;
			currentTab = "Player" ;
			mc.Send_mc.visible = false ;
			mc.Admin_mc.visible = false ;
			mc.Transfer_mc.visible = false ;
			mc.Player_mc.visible = true ;
			mc.AdminTitle_mc.visible = false ;
			mc.TransferTitle_mc.visible = false ;
			mc.PlayerTitle_mc.visible = true ;
			mc.MailDetail_mc.visible = true ;
			mc.MailDetail_mc.Id_txt.text = "" ;
			mc.MailDetail_mc.From_txt.text = "" ;
			mc.MailDetail_mc.Content_txt.text = "" ;
			mc.MailDetail_mc.Time_txt.text = "" ;
			mc.MailDetail_mc.AG_txt.text = "0" ;
			mc.MailDetail_mc.Reply_btn.visible = false ;
			mc.MailDetail_mc.ReceiveAG_btn.visible = false ;
			mc.Send_btn.gotoAndStop(1) ;
			mc.Admin_btn.gotoAndStop(1) ;
			mc.Transfer_btn.gotoAndStop(1) ;
			mc.Player_btn.gotoAndStop(2) ;
			var obj : Object = new Object() ;
			obj.evt = "15" ;
			obj.T = 5;
			obj.P = currentPagePlayer ;
			Cocaro.mainSocket.sendService(JSON.encode(obj)) ;
			//Cocaro.tf.appendText("=>SendMailPlayer:" + JSON.encode(obj)) ;
		}
		/******************** Receive Data **************************/
		public static function MailAdmin(str : String) {
			//Cocaro.tf.appendText("=>ReMailAdmin:" + str) ;
			var arrMail : Array = JSON.decode(str) as Array ;
			var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Mail")) ;
			if (mc == null) return ;
			mc.TotalPage_txt.text = totalPageAdmin ;
			mc.CurrentPage_txt.text = currentPageAdmin ;
			if (arrMailAdmin != null) {
				for (var j:int = 0; j < arrMailAdmin.length; j++) 
				{
					var mcRemove : MovieClip = MovieClip(mc.Admin_mc.getChildByName("MailAdmin_" + arrMailAdmin[j].Id));
					if (mcRemove != null)
						mc.Admin_mc.removeChild(mcRemove) ;
				}
			}
			arrMailAdmin = new Array() ;
			for (var i:int = 0; i < arrMail.length; i++) 
			{
				if (arrMail[i].D == true) continue ;
				var obj : Object = new Object();
				obj.Id = arrMail[i].Id ;
				obj.From = arrMail[i].From ;
				obj.To = arrMail[i].To ;
				obj.AG = arrMail[i].AG ;
				obj.Msg = arrMail[i].Msg ;
				obj.Time = arrMail[i].Time ;
				obj.T = arrMail[i].T ;
				obj.S = arrMail[i].S ;
				obj.chk = false ;
				////Cocaro.tf.appendText("==>Mail1:" + arrMailAdmin.length) ;
				var mcMailItem : MovieClip = MovieClip(new Mail_Item_Admin()) ;
				mcMailItem.name = "MailAdmin_" + obj.Id ;
				mcMailItem.x = 2 ;
				mcMailItem.y = 2 + arrMailAdmin.length * 22 ;
				mcMailItem.Content_txt.text = obj.Msg ;
				var formatText = new TextFormat() ;				
				formatText.font = "Arial" ;
				formatText.bold = true ;
				formatText.size = 12 ;
				if (obj.S == false)
					mcMailItem.Content_txt.setTextFormat(formatText) ;
				else {
					formatText.bold = false ;
					formatText.color = 0xE5DFE9 ;
					mcMailItem.Content_txt.setTextFormat(formatText) ;
				}
				mcMailItem.Time_txt.text = BasicFunction.convertTimeToString(obj.Time) ;
				new MailItemController(mcMailItem, obj) ;
				arrMailAdmin.push(obj) ;
				mc.Admin_mc.addChild(mcMailItem) ;
			}
		}
		public static function MailPlayer(str : String) {
			//Cocaro.tf.appendText("=>ReMailPlayer:" + str) ;
			var arrMail : Array = JSON.decode(str) as Array ;
			var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Mail")) ;
			if (mc == null) return ;			
			mc.TotalPage_txt.text = totalPagePlayer ;
			mc.CurrentPage_txt.text = currentPagePlayer ;
			if (arrMailPlayer != null) {
				for (var j:int = 0; j < arrMailPlayer.length; j++) 
				{
					var mcRemove : MovieClip = MovieClip(mc.Player_mc.getChildByName("MailPlayer_" + arrMailPlayer[j].Id));
					if (mcRemove != null)
						mc.Player_mc.removeChild(mcRemove) ;
				}
			}
			arrMailPlayer = new Array() ;
			for (var i:int = 0; i < arrMail.length; i++) 
			{
				if (arrMail[i].D == true) continue ;
				var obj : Object = new Object();
				obj.Id = arrMail[i].Id ;
				obj.From = arrMail[i].From ;
				obj.To = arrMail[i].To ;
				obj.AG = arrMail[i].AG ;
				obj.Msg = arrMail[i].Msg ;
				obj.Time = arrMail[i].Time ;
				obj.T = arrMail[i].T ;
				obj.S = arrMail[i].S ;
				obj.chk = false ;
				////Cocaro.tf.appendText("==>Mail1:" + arrMailPlayer.length) ;
				var mcMailItem : MovieClip = MovieClip(new Mail_Item()) ;
				mcMailItem.name = "MailPlayer_" + obj.Id ;
				mcMailItem.x = 0 ;
				mcMailItem.y = arrMailPlayer.length * 22 ;
				mcMailItem.Name_txt.text = obj.From ;
				mcMailItem.Content_txt.text = obj.Msg ;
				var formatText = new TextFormat() ;				
				formatText.font = "Arial" ;
				formatText.bold = true ;
				formatText.size = 12 ;
				if (obj.S == false)
					mcMailItem.Content_txt.setTextFormat(formatText) ;
				else {
					formatText.bold = false ;
					formatText.color = 0xE5DFE9 ;
					mcMailItem.Content_txt.setTextFormat(formatText) ;
				}
				mcMailItem.Time_txt.text = BasicFunction.convertTimeToString(obj.Time) ;
				new MailItemController(mcMailItem, obj) ;
				arrMailPlayer.push(obj) ;
				mc.Player_mc.addChild(mcMailItem) ;
			}
		}
		
		public static function MailTransfer(str : String) {
			//Cocaro.tf.appendText("=>ReMailTransfer:" + str) ;
			var arrMail : Array = JSON.decode(str) as Array ;
			var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Mail")) ;
			if (mc == null) return ;			
			mc.TotalPage_txt.text = totalPageTransfer ;
			mc.CurrentPage_txt.text = currentPageTransfer ;
			if (arrMailTransfer != null) {
				for (var j:int = 0; j < arrMailTransfer.length; j++) 
				{
					var mcRemove : MovieClip = MovieClip(mc.Transfer_mc.getChildByName("MailTransfer_" + arrMailTransfer[j].Id));
					if (mcRemove != null)
						mc.Transfer_mc.removeChild(mcRemove) ;
				}
			}
			//MailTransfer_List = new MovieClip() ;
			arrMailTransfer = new Array() ;
			for (var i:int = 0; i < arrMail.length; i++) 
			{
				if (arrMail[i].D == true) continue ;
				var obj : Object = new Object();
				obj.Id = arrMail[i].Id ;
				obj.From = arrMail[i].From ;
				obj.To = arrMail[i].To ;
				obj.AG = arrMail[i].AG ;
				obj.Msg = arrMail[i].Msg ;
				obj.Time = arrMail[i].Time ;
				obj.T = arrMail[i].T ;
				obj.S = arrMail[i].S ;
				obj.chk = false ;				
				var mcMailItem : MovieClip = MovieClip(new Mail_Item()) ;
				mcMailItem.name = "MailTransfer_" + obj.Id;
				mcMailItem.x = 1 ;
				mcMailItem.y = 2 + arrMailTransfer.length * 22 ;
				mcMailItem.Name_txt.text = obj.From ;
				mcMailItem.Content_txt.text = obj.Msg ;
				var formatText = new TextFormat() ;				
				formatText.font = "Arial" ;
				formatText.bold = true ;
				formatText.size = 12 ;
				if (obj.S == false)
					mcMailItem.Content_txt.setTextFormat(formatText) ;
				else {
					formatText.bold = false ;
					formatText.color = 0xE5DFE9 ;
					mcMailItem.Content_txt.setTextFormat(formatText) ;
				}
				mcMailItem.Time_txt.text = BasicFunction.convertTimeToString(obj.Time) ;
				new MailItemController(mcMailItem, obj) ;
				mc.Transfer_mc.addChild(mcMailItem) ;
				arrMailTransfer.push(obj) ;
			}
		}
		public static function Mail_Click(evt : MouseEvent) {
			Cocaro.SoundKick() ;
			if ((Cocaro.mainStage_mc.currentFrame != 3) && (Cocaro.mainStage_mc.currentFrame != 4)) return ;
			var mc : MovieClip = MovieClip(new Popup_Mail()) ;
			mc.Send_btn.addEventListener(MouseEvent.CLICK, Send_Click) ;
			mc.Send_mc.Send_btn.addEventListener(MouseEvent.CLICK, SendMail_Click) ;
			mc.Admin_btn.addEventListener(MouseEvent.CLICK, Admin_Click) ;
			mc.Transfer_btn.addEventListener(MouseEvent.CLICK, Transfer_Click) ;
			mc.Player_btn.addEventListener(MouseEvent.CLICK, Player_Click) ;
			mc.Close_btn.addEventListener(MouseEvent.CLICK, Close_click);
			mc.Send_mc.visible = true ;
			mc.Admin_mc.visible = true ;
			mc.Transfer_mc.visible = false ;
			mc.Player_mc.visible = false ;
			mc.AdminTitle_mc.visible = true ;
			mc.TransferTitle_mc.visible = false ;
			mc.PlayerTitle_mc.visible = false ;
			mc.MailDetail_mc.visible = false ;
			mc.TransferTitle_mc.DeleteAll_chk.addEventListener(MouseEvent.CLICK, ChkAllTransfer_Click) ;
			mc.PlayerTitle_mc.DeleteAll_chk.addEventListener(MouseEvent.CLICK, ChkAllPlayer_Click) ;
			mc.AdminTitle_mc.DeleteAll_chk.addEventListener(MouseEvent.CLICK, ChkAllAdmin_Click) ;
			mc.DeleteAll_btn.addEventListener(MouseEvent.CLICK, DeleteAllMail_Click) ;
			mc.Send_btn.gotoAndStop(2) ;
			mc.Admin_btn.gotoAndStop(1) ;
			mc.Transfer_btn.gotoAndStop(1) ;
			mc.Player_btn.gotoAndStop(1) ;
			mc.MailDetail_mc.Reply_btn.addEventListener(MouseEvent.CLICK, ReplyMail_Click) ;
			mc.MailDetail_mc.ReceiveAG_btn.addEventListener(MouseEvent.CLICK, ReceiveAG_Click) ;
			var tfStyle:TextFormat = new TextFormat();
			tfStyle.size = 12;
			tfStyle.color = 0xFFFFFF;
			tfStyle.font = "Arial";
			mc.Send_mc.To_txt.setStyle("textFormat", tfStyle);
			mc.MailDetail_mc.From_txt.setStyle("textFormat", tfStyle);
			mc.PreviousEnd_btn.addEventListener(MouseEvent.CLICK, PreviousEnd_Click) ;
			mc.Previous_btn.addEventListener(MouseEvent.CLICK, Previous_Click) ;
			mc.NextEnd_btn.addEventListener(MouseEvent.CLICK, NextEnd_Click) ;
			mc.Next_btn.addEventListener(MouseEvent.CLICK, Next_Click) ;
			
			arrMailAdmin = new Array() ;
			arrMailTransfer = new Array() ;
			arrMailPlayer = new Array() ;			
			
			var obj : Object = new Object() ;
			obj.evt = "15" ;
			obj.T = 4 ;
			obj.P = 1 ;
			Cocaro.mainSocket.sendService(JSON.encode(obj)) ;
			
			currentPageAdmin = 1 ;
			currentPagePlayer = 1 ;
			currentPageTransfer = 1 ;
			totalPageAdmin = 2 ;
			totalPagePlayer = 1 ;
			totalPageTransfer = 1 ;
			mc.CurrentPage_txt.text = currentPageAdmin ;
			currentTab = "Admin" ;
			
			mc.name = "Mail" ;
			mc.x = 0 ;
			mc.y = 0 ;
			Cocaro.mainStage_mc.addChild(mc) ;
		}
		
		public static function ChkAllTransfer_Click(evt : MouseEvent) {
			//Cocaro.tf.appendText("=>110") ;
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Mail")) ;
			if (popup == null) return ;
			//Cocaro.tf.appendText("=>1") ;
			//var mcList : MovieClip = MovieClip(popup.Transfer_mc.getChildByName("MailTransfer")) ;
			//if (mcList == null) return ;
			//Cocaro.tf.appendText("=>2") ;
			for (var i:int = 0; i < arrMailTransfer.length; i++) 
			{
				//Cocaro.tf.appendText("=>Id:" + arrMailTransfer[i].Id) ;
				var mc : MovieClip = MovieClip(popup.Transfer_mc.getChildByName("MailTransfer_" + arrMailTransfer[i].Id)) ;
				mc.Delete_chk.selected = popup.TransferTitle_mc.DeleteAll_chk.selected ;
				arrMailTransfer[i].chk = popup.TransferTitle_mc.DeleteAll_chk.selected ;
			}
			//Cocaro.tf.appendText("=>3") ;	
		}
		
		public static function ChkAllPlayer_Click(evt : MouseEvent) {
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Mail")) ;
			if (popup == null) return ;
			//var mcList : MovieClip = MovieClip(popup.Player_mc.getChildByName("MailPlayer")) ;
			//if (mcList == null) return ;
			for (var i:int = 0; i < arrMailPlayer.length; i++) 
			{
				var mc : MovieClip = MovieClip(popup.Player_mc.getChildByName("MailPlayer_" + arrMailPlayer[i].Id)) ;
				mc.Delete_chk.selected = popup.PlayerTitle_mc.DeleteAll_chk.selected ;
				arrMailPlayer[i].chk = popup.PlayerTitle_mc.DeleteAll_chk.selected ;
			}
		}
		
		public static function ChkAllAdmin_Click(evt : MouseEvent) {
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Mail")) ;
			if (popup == null) return ;
			//var mcList : MovieClip = MovieClip(popup.Admin_mc.getChildByName("MailAdmin")) ;
			//if (mcList == null) return ;
			for (var i:int = 0; i < arrMailAdmin.length; i++) 
			{
				var mc : MovieClip = MovieClip(popup.Admin_mc.getChildByName("MailAdmin_" + arrMailAdmin[i].Id)) ;
				mc.Delete_chk.selected = popup.AdminTitle_mc.DeleteAll_chk.selected ;
				arrMailAdmin[i].chk = popup.AdminTitle_mc.DeleteAll_chk.selected ;
			}
		}
		
		public static function DeleteAllMail_Click(evt : MouseEvent) {
			var popup : MovieClip = MovieClip(new Popup_Confirm()) ;
			popup.Title_txt.text = "Xóa Thư" ;
			popup.Content_txt.text = "Bạn có chắc chắn muốn xóa thư?" ;
			popup.OK_btn.addEventListener(MouseEvent.CLICK, OK_DeleteAll_Click) ;
			popup.NotOK_btn.addEventListener(MouseEvent.CLICK, NotOK_Delete_Click) ;
			popup.name = "DeleteConfirm" ;
			popup.x = 0 ;
			popup.y = 0 ;
			Cocaro.mainStage_mc.addChild(popup) ;			
		}
		
		public static function PreviousEnd_Click(evt : MouseEvent) {
			if ((currentTab == "Admin") && (currentPageAdmin <= 1)) return ;
			if ((currentTab == "Transfer") && (currentPageTransfer <= 1)) return ;
			if ((currentTab == "Player") && (currentPagePlayer <= 1)) return ;
			var obj : Object = new Object() ;
			obj.evt = "15" ;
			if (currentTab == "Admin") {
				currentPageAdmin = 1 ;
				obj.T = 4;
				obj.P = currentPageAdmin ;
			} else if (currentTab == "Transfer") {
				currentPageTransfer = 1 ;
				obj.T = 6;
				obj.P = currentPageTransfer ;
			} else if (currentTab == "Player") {
				currentPagePlayer = 1 ;
				obj.T = 5;
				obj.P = currentPagePlayer ;
			}
			Cocaro.mainSocket.sendService(JSON.encode(obj)) ;
			//Cocaro.tf.appendText("=>SendMailAdmin:" + JSON.encode(obj)) ;
		}
		public static function Previous_Click(evt : MouseEvent) {
			if ((currentTab == "Admin") && (currentPageAdmin == 1)) return ;
			if ((currentTab == "Transfer") && (currentPageTransfer == 1)) return ;
			if ((currentTab == "Player") && (currentPagePlayer == 1)) return ;
			var obj : Object = new Object() ;
			obj.evt = "15" ;
			if (currentTab == "Admin") {
				currentPageAdmin--  ;
				obj.T = 4;
				obj.P = currentPageAdmin ;
			} else if (currentTab == "Transfer") {
				currentPageTransfer--  ;
				obj.T = 6;
				obj.P = currentPageTransfer ;
			} else if (currentTab == "Player") {
				currentPagePlayer--  ;
				obj.T = 5;
				obj.P = currentPagePlayer ;
			}			
			Cocaro.mainSocket.sendService(JSON.encode(obj)) ;
			//Cocaro.tf.appendText("=>SendMailAdmin:" + JSON.encode(obj)) ;
		}
		public static function NextEnd_Click(evt : MouseEvent) {
			if ((currentTab == "Admin") && (currentPageAdmin >= totalPageAdmin)) return ;
			if ((currentTab == "Transfer") && (currentPageTransfer >= totalPageTransfer)) return ;
			if ((currentTab == "Player") && (currentPagePlayer >= totalPagePlayer)) return ;
			var obj : Object = new Object() ;
			obj.evt = "15" ;
			if (currentTab == "Admin") {
				currentPageAdmin = totalPageAdmin ;
				obj.T = 4;
				obj.P = currentPageAdmin ;
			} else if (currentTab == "Transfer") {
				currentPageTransfer = totalPageTransfer ;
				obj.T = 6;
				obj.P = currentPageTransfer ;
			} else if (currentTab == "Player") {
				currentPagePlayer = totalPagePlayer ;
				obj.T = 5;
				obj.P = currentPagePlayer ;
			}
			Cocaro.mainSocket.sendService(JSON.encode(obj)) ;
			//Cocaro.tf.appendText("=>SendMailAdmin:" + JSON.encode(obj)) ;
		}
		public static function Next_Click(evt : MouseEvent) {
			if ((currentTab == "Admin") && (currentPageAdmin >= totalPageAdmin)) return ;
			if ((currentTab == "Transfer") && (currentPageTransfer >= totalPageTransfer)) return ;
			if ((currentTab == "Player") && (currentPagePlayer >= totalPagePlayer)) return ;
			var obj : Object = new Object() ;
			obj.evt = "15" ;
			if (currentTab == "Admin") {
				currentPageAdmin++  ;
				obj.T = 4;
				obj.P = currentPageAdmin ;
			} else if (currentTab == "Transfer") {
				currentPageTransfer++  ;
				obj.T = 6;
				obj.P = currentPageTransfer ;
			} else if (currentTab == "Player") {
				currentPagePlayer++  ;
				obj.T = 5;
				obj.P = currentPagePlayer ;
			}
			Cocaro.mainSocket.sendService(JSON.encode(obj)) ;
			//Cocaro.tf.appendText("=>SendMailAdmin:" + JSON.encode(obj)) ;
		}
		
		public static function OK_DeleteAll_Click(evt : MouseEvent) {
			//Cocaro.tf.appendText("=>currentTab:" + currentTab) ; 
			var mcRemove : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("DeleteConfirm")) ;
			while (mcRemove != null) {
				Cocaro.mainStage_mc.removeChild(mcRemove) ;
				mcRemove = MovieClip(Cocaro.mainStage_mc.getChildByName("DeleteConfirm")) ;
			}
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Mail")) ;
			if (popup == null) return ;
			var obj : Object = new Object() ;
			obj.evt = "15" ;
			obj.T = 3 ;
			var arr : Array = new Array() ;			
			if (currentTab == "Admin") {
				for (var i:int = 0; i < arrMailAdmin.length; i++) 
				{
					if (arrMailAdmin[i].chk) {
						arr.push(arrMailAdmin[i].Id) ;
						if (arrMailAdmin[i].S == false)
							Cocaro.currPlayer.CountMsg-- ;
					}
				}
			} else if (currentTab == "Transfer") {
				for (var i:int = 0; i < arrMailTransfer.length; i++) 
				{
					if (arrMailTransfer[i].chk) {
						arr.push(arrMailTransfer[i].Id) ;
						if (arrMailTransfer[i].S == false)
							Cocaro.currPlayer.CountMsg-- ;
					}
				}
			} else if (currentTab == "Player") {
				for (var i:int = 0; i < arrMailPlayer.length; i++) 
				{
					if (arrMailPlayer[i].chk) {
						arr.push(arrMailPlayer[i].Id) ;
						if (arrMailPlayer[i].S == false)
							Cocaro.currPlayer.CountMsg-- ;
					}
				}
			}
			obj.Arr = arr ;
			if (arr.length == 0) {
				BasicFunction.ShowAlert("Bạn phải chọn thư xóa!") ;
				return ;
			}
			Cocaro.mainSocket.sendService(JSON.encode(obj)) ;
			//Cocaro.tf.appendText("==>SendDelMail:" + JSON.encode(obj));
			//Xoa giao dien
			var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Mail")) ;
			if (mc == null) return ;
			mc.MailDetail_mc.From_txt.text = "" ;
			mc.MailDetail_mc.Content_txt.text = "" ; 
			mc.MailDetail_mc.Time_txt.text = "" ;			
			mc.MailDetail_mc.Id_txt.text = 0 ;
			mc.MailDetail_mc.AG_txt.text = "" ;
			var obj : Object = new Object() ;
			obj.evt = "15" ;
			if (currentTab == "Admin") {					
				obj.T = 4;
				obj.P = currentPageAdmin ;
			} else if (currentTab == "Player") {
				obj.T = 5;
				obj.P = currentPagePlayer ;
			} else if (currentTab == "Transfer") {
				obj.T = 6;
				obj.P = currentPageTransfer ;
			}
			Cocaro.mainSocket.sendService(JSON.encode(obj)) ;			
			////Cocaro.tf.appendText("==>SendDelMail:" + JSON.encode(obj));
		}
		
		public static function NotOK_Delete_Click(evt : MouseEvent) {
			var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("DeleteConfirm")) ;
			while (mc != null) {
				Cocaro.mainStage_mc.removeChild(mc) ;
				mc = MovieClip(Cocaro.mainStage_mc.getChildByName("DeleteConfirm")) ;
			}
		}
		
		public static function ReplyMail_Click(evt : MouseEvent) {
			var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Mail")) ;
			if (mc == null) return ;
			var from : String = mc.MailDetail_mc.From_txt.text ;
			mc.MailDetail_mc.visible = false ;
			mc.Send_mc.visible = true ;
			mc.Send_mc.To_txt.text = from ;			
		}
		
		public static function ReceiveAG_Click(evt : MouseEvent) {
			var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Mail")) ;
			if (mc == null) return ;
			if (BasicFunction.trim(mc.MailDetail_mc.Id_txt.text) == "") {
				BasicFunction.ShowAlert("Chọn thư để nhận AG") ;
				return ;
			}
			var obj :Object = new Object() ;
			obj.evt = "15" ;
			obj.T = 2 ;
			arrReceiveAG = new Array() ;
			arrReceiveAG.push(parseInt(mc.MailDetail_mc.Id_txt.text)) ;
			obj.Arr = arrReceiveAG ;
			Cocaro.mainSocket.sendService(JSON.encode(obj)) ;
			//Cocaro.tf.appendText("==>SendReceiveAG:" + JSON.encode(obj));
			var tmpLD:MovieClip = MovieClip(new Loading());
			tmpLD.name = "Loading";
			tmpLD.x = 0;
			tmpLD.y = 0;
			Cocaro.mainStage_mc.addChild(tmpLD);
		}
		
		public static function ReceiveAG(obj : Object) {
			BasicFunction.RemoveHideLoading() ;
			var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Mail")) ;
			if (mc == null) return ;
			//Cocaro.tf.appendText("==>ReceiveAG:" + obj.AG) ;
			mc.MailDetail_mc.ReceiveAG_btn.visible = false ;
			mc.MailDetail_mc.AG_txt.text = "0" ;
			if (obj.AG > 0) {
				for (var i:int = 0; i < arrReceiveAG.length; i++) 
				{
					for (var j:int = 0; j < arrMailAdmin.length; j++) 
					{
						if (arrReceiveAG[i] == arrMailAdmin[j].Id) {
							var mcRemove : MovieClip = MovieClip(mc.Admin_mc.getChildByName("MailAdmin_" + arrMailAdmin[j].Id));
							if (mcRemove != null)
								mc.Admin_mc.removeChild(mcRemove) ;
							for (var m:int = j+1; m < arrMailAdmin.length; m++) 
							{
								mcRemove = MovieClip(mc.Admin_mc.getChildByName("MailAdmin_" + arrMailAdmin[m].Id));
								if (mcRemove != null)
									mcRemove.y = mcRemove.y - 22 ;
							}
							arrMailAdmin.splice(j,1) ;
							break ;
						}
					}
					for (var k:int = 0; k < arrMailPlayer.length; k++) 
					{
						if (arrReceiveAG[i] == arrMailPlayer[k].Id) {
							var mcRemove : MovieClip = MovieClip(mc.Player_mc.getChildByName("MailPlayer_" + arrMailPlayer[k].Id));
							if (mcRemove != null)
								mc.Player_mc.removeChild(mcRemove) ;
							for (var m:int = k+1; m < arrMailPlayer.length; m++) 
							{
								mcRemove = MovieClip(mc.Player_mc.getChildByName("MailPlayer_" + arrMailPlayer[m].Id));
								if (mcRemove != null)
									mcRemove.y = mcRemove.y - 22 ;
							}
							arrMailPlayer.splice(k,1) ;
							break ;
						}
					}
					for (var l:int = 0; l < arrMailTransfer.length; l++) 
					{
						if (arrReceiveAG[i] == arrMailTransfer[l].Id) {
							var mcRemove : MovieClip = MovieClip(mc.Transfer_mc.getChildByName("MailTransfer_" + arrMailTransfer[l].Id));
							if (mcRemove != null)
								mc.Transfer_mc.removeChild(mcRemove) ;
							for (var m:int = l+1; m < arrMailTransfer.length; m++) 
							{
								mcRemove = MovieClip(mc.Transfer_mc.getChildByName("MailTransfer_" + arrMailTransfer[m].Id));
								if (mcRemove != null)
									mcRemove.y = mcRemove.y - 22 ;
							}
							arrMailTransfer.splice(j,1) ;
							break ;
						}
					}
				}
				Cocaro.currPlayer.Mark = Cocaro.currPlayer.Mark + obj.AG ;
				if ((Cocaro.mainStage_mc.currentFrame == 4) || (Cocaro.mainStage_mc.currentFrame == 3)) {
					Cocaro.mainStage_mc.Profile_mc.Mark_txt.text = BasicFunction.numberFormat(Cocaro.currPlayer.Mark, 0, false, true) + " AG";
					Cocaro.mainStage_mc.Profile_mc.Mark_txt.setTextFormat(Cocaro.formatTextMain) ;
				}
				BasicFunction.ShowAlert("Bạn đã được nhận " + obj.AG + " AG!") ;
			}			
		}
		
		public static function SendMail_Click(evt : MouseEvent) {
			var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Mail")) ;
			if (mc == null) return ;
			if (BasicFunction.trim(mc.Send_mc.To_txt.text) == "") {
				BasicFunction.ShowAlert("Địa chỉ gửi phải khác rỗng!") ;
				return ;
			}
			if (BasicFunction.trim(mc.Send_mc.Content_txt.text) == "") {
				BasicFunction.ShowAlert("Nội dung gửi phải khác rỗng!") ;
				return ;
			}
			var obj : Object = new Object() ;
			obj.evt = "15" ;
			obj.T = 7 ;
			obj.NN = mc.Send_mc.To_txt.text ;
			obj.D = mc.Send_mc.Content_txt.text ;
			Cocaro.mainSocket.sendService(JSON.encode(obj)) ;
			//Cocaro.tf.appendText("==>ABC:" + JSON.encode(obj));
			mc.Send_mc.To_txt.text = "" ;
			mc.Send_mc.Content_txt.text = "" ;
		}
		
		public static function Close_click(evt:MouseEvent) { //Xu ly Kick Close tren popup chuyen tien
			Cocaro.SoundKick() ;
			var tmpLD : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Loading"));
			while (tmpLD != null) {
				Cocaro.mainStage_mc.removeChild(tmpLD) ;
				tmpLD = MovieClip(Cocaro.mainStage_mc.getChildByName("Loading"));
			}
			var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Mail")) ;
			if (mc == null) return ;
			Cocaro.mainStage_mc.removeChild(mc) ;
			if (Cocaro.currPlayer.CountMsg > 0) {
				Cocaro.mainStage_mc.Mail_btn.CountMsg_txt.text = Cocaro.currPlayer.CountMsg ;
				Cocaro.mainStage_mc.Mail_btn.CountMsg_txt.visible = true ;
				Cocaro.mainStage_mc.Mail_btn.gotoAndStop(2) ;
			} else {
				Cocaro.mainStage_mc.Mail_btn.CountMsg_txt.visible = false ;
				Cocaro.mainStage_mc.Mail_btn.gotoAndStop(1) ;
			}
		}
		public static function NewMail() { //Co them 1 thu moi
			//Cocaro.tf.appendText("=>Co thu moi") ;
			Cocaro.currPlayer.CountMsg ++ ;
			if ((Cocaro.mainStage_mc.currentFrame == 3) || (Cocaro.mainStage_mc.currentFrame == 4)) {
				Cocaro.mainStage_mc.Mail_btn.CountMsg_txt.text = Cocaro.currPlayer.CountMsg ;
				Cocaro.mainStage_mc.Mail_btn.CountMsg_txt.visible = true ;
				Cocaro.mainStage_mc.Mail_btn.gotoAndStop(2) ;
			}				
		}
	}
	
}